package com.qfedu.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.BoatEntity;
import com.qfedu.entity.BoatTypeEntity;
import com.qfedu.entity.RouteEntity;
import com.qfedu.mapper.BoatMapper;
import com.qfedu.service.BoatService;
import com.qfedu.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@com.alibaba.dubbo.config.annotation.Service
public class BoatServiceimpl implements BoatService {

    @Autowired(required = false)
    private BoatMapper boatMapper;

    @Reference(check = false)
    RouteService routeService;

    @Override
    public List<BoatEntity> queryByRid(int rid) {
        List<BoatEntity> boatEntities = boatMapper.queryByRid(rid);
        return boatEntities;
    }

    public List<BoatEntity> queryBoat(){
        return boatMapper.queryBoat();
    }

    public boolean insert(int rid,int sid){
        return boatMapper.insert(rid,sid);
    }

    public PageInfo<BoatEntity> queryBoatList(BoatEntity be){

        PageHelper.startPage(be.getPageNum(), be.getPageSize());

        List<BoatEntity> list =boatMapper.queryBoatList(be);

        for (BoatEntity belist:list){

            if (belist.getRid() != 0){
                RouteEntity routeEntity = routeService.queryRouteById(belist.getRid());
                if (routeEntity != null){
                    belist.setRe(routeEntity);
                }

            }else{

            }

        }

        PageInfo<BoatEntity> pageInfo = new PageInfo<>(list);

        return pageInfo;


    }

    public boolean delBoat(int sid){

        String str = "("+sid+")";

        return boatMapper.delBoat(str);
    }


    public boolean deAllBoat(String ids){

        String str = "("+ids.substring(0,ids.length()-1)+")";

        return boatMapper.delBoat(str);
    }


    public boolean updateBoat(BoatEntity be){

        routeService.insertBoatRoute(be.getRid(), be.getSid());

        return boatMapper.updateBoat(be);
    }


    public boolean insertBoat(BoatEntity be){

        be.setSstate(2);

        boolean insert = boatMapper.insertBoat(be);

        routeService.insertBoatRoute(be.getRid(), be.getSid());

        if (insert && insert){
            return true;
        }else {
            return false;
        }

    }

    public  List<BoatTypeEntity> queryBoatType(){
        return boatMapper.queryBoatType();
    }

    public  BoatEntity queryBoatById(int sid){
        return boatMapper.queryBoatById(sid);
    }

}
